import Vue from 'vue'
// 引入router
import Router from 'vue-router'
// 注册router
Vue.use(Router)

/* Layout */
// 导入layout（布局）组件
import Layout from '@/layout'
// 引入多个模块规则
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

// 配置路由规则 静态路由
export const constantRoutes = [
  {
    path: '/login', // 配置路径
    component: () => import('@/views/login/index'), // 路由懒加载  按需加载，提高性能(访问路径，才会执行函数拿到组件)
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    /*
    layout没有懒加载，布局组件一般不用懒加载
    只有用户需要进行一些操作才能看到的页面，可以使用懒加载
    */
    component: Layout,
    // 重定向到首页，配置二级路由
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },
  {
    path: '/import',
    component: Layout,
    hidden: true,
    children: [{
      path: '', // 二级路由path什么都不写 表示二级默认路由
      component: () => import('@/views/import/index')
    }]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]
// 动态路由表 => 动态路由(需要权限才可以访问的) 我们这里准备一个数组存放
export const asyncRoutes = [
  departmentsRouter,
  settingRouter,
  employeesRouter,
  permissionRouter,
  approvalsRouter,
  attendancesRouter,
  salarysRouter,
  socialRouter
]
// createRouter是个箭头函数，函数执行后创建实例
// 以前都是直接new Router，为什么这里要用函数包裹
// 函数 - 封装复用 - 这里用函数包裹为了复用
const createRouter = () => new Router({
  // mode: 'history', // require service support
  // 滚动行为，它让你可以自定义路由切换时页面如何滚动
  // 返回对象 属性x和y标记的是滚动条的位置
  scrollBehavior: () => ({ y: 0 }),
  // 完整写法
  // scrollBehavior(){
  //   return {
  //     y:0
  //   }
  // }
  routes: [...constantRoutes, ...asyncRoutes] // 以前直接在这里配置数组，现在提了个变量
})

// 调用箭头函数，返回值是个路由实例
const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465

/*
  重置路由
  1.必须重新实例化
  2.必须覆盖router的属性matcher

  重置的应用场景
  做权限功能设置时，假设A员工对饮的角色有A/B/C/D页面的访问权限
  A员工退出登录，登录B员工
  B员工对应的页面权限只有A/B/C，但是此时不重置路由，他通过URL还可以访问D页面，所以需要重置路由
*/
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
